#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    socket = new QTcpSocket;//创建socket对象

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_cancelButton_clicked()
{
    this->close();
}

void Widget::on_connectButton_clicked()
{
    //获取ip地址和端口号
    QString IP = ui->iplineEdit->text();
    QString Port = ui->portlineEdit->text();

    //qDebug()<<IP;
    //qDebug()<<Port;

    //与服务器发起连接(QHostAddress ip,quint16 port)
    socket->connectToHost(QHostAddress(IP),Port.toShort());

    //连接服务器成功，socket对象发出信号
    connect(socket,&QTcpSocket::connected,[this]()
    {
        QMessageBox::information(this,"连接提示","连接服务器成功");

        //#启动新界面
        this->hide();//#隐藏这个界面
        Chat *Ch = new Chat(socket);//#在堆空间创建对象 将socket作为参数也传过去
        Ch->show();

    });

    //连接断开，socket会发出信号
    connect(socket,&QTcpSocket::disconnected,[this]()
    {
        QMessageBox::information(this,"连接提示","连接异常，网络断开");
    });
}
